package nicg.st;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.bingqi.Info;
import com.bingqi.MsgTrans1;
import com.bingqi.P2PCallback;

import nicg.st.util.RecevieUtil;

@Component
public class Communicate implements CommandLineRunner {

	/**
	 * 传输服务状态码
	 */
	public static int communicateCode;
	/**
	 * 实时评估服务状态码
	 */
	public static int evalCode;
	/**
	 * 服务开启成功码
	 */
	public static int SERVICE_SUCCESS = 000;
	/**
	 * 服务开启失败码
	 */
	public static int SERVICE_FAILURE = 001;
	/**
	 * 分队的身份ID
	 */
	public static int IDENTITY_UNIT_ID = 130;
	/**
	 * 旅的身份ID
	 */
	public static int IDENTITY_BRIGADE_ID = 131;
	/**
	 * 软件ID
	 */
	public static int SOFT_ID = 9510;
	/**
	 * 定义接受数据的回调
	 */
	public static final P2PCallback callback = new P2PCallback() {
		@Override
		public void pCallback(Info.srcUsrInfo srcUsrInfo, Info.dataType dataType, int serinalNum,
				Info.transAttr transAttr, Info.ackAttr ackAttr, String data, int dataLen, String codeData,
				int iCodeDataLen, String fileName, String oriFileName, int retlen, String retmsg) {
			if (!ackAttr.isack) {
				RecevieUtil recevieUtil = new RecevieUtil();
				recevieUtil.receiveFile(data, srcUsrInfo);
			}
		}
	};

	public static int getIdentityId() {
		return 130;
	}

	/**
	 * 开启信息传输服务
	 */
	public static void startCommunicateService() {
		int res = MsgTrans1.init_();
		if (res != 0) {
			System.out.println("传输服务开启失败");
			Communicate.communicateCode = Communicate.SERVICE_FAILURE;
		} else {
			System.out.println("传输服务开启成功");
			Communicate.communicateCode = Communicate.SERVICE_SUCCESS;
		}
		if (Communicate.getIdentityId() == Communicate.IDENTITY_UNIT_ID) {
			int p2pFlag = MsgTrans1.p2pReg_(Communicate.getIdentityId(), Communicate.SOFT_ID, Communicate.callback);
			if (p2pFlag == 0) {
				Communicate.communicateCode = Communicate.SERVICE_SUCCESS;
				System.out.println("点对点注册成功");
			} else {
				Communicate.communicateCode = Communicate.SERVICE_FAILURE;
				System.out.println("点对点注册失败");
			}
		} else {

		}
	}

	/**
	 * 开启实时评估服务
	 */
	public static void startEvalService() {

	}

	@Override
	public void run(String... arg0) throws Exception {
		// 联调时打开注释
//		Communicate.startCommunicateService();
//		Communicate.startEvalService();
	}

}
